//
//  smcRoundedFlatBox.m
//  juhu
//
//  Created by Christian Martick on 20.04.07.
//  Copyright 2007 Christian Martick All rights reserved.
//
//	Subclassing NSBox to draw round cornered gray box (color changable...)
//
//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcRoundedFlatBox.h"


@implementation smcRoundedFlatBox

- (void) dealloc 
{
	[backgroundColor release];
	[image release];
	[imageBot release];
	[imageTop release];
	[super dealloc];
}

- (void) awakeFromNib
{
	//gray is standard
	if (backgroundColor == nil)
		backgroundColor = [[NSColor colorWithCalibratedWhite: 0.7 alpha: 0.95] retain];
}

- (BOOL) becomeFirstResponder
{
	//NSLog(@"first responder");
	return [super becomeFirstResponder];
}

- (BOOL)mouseDownCanMoveWindow
{
	// to make it move!
	return YES;
}

- (void) setBackgroundColor: (NSColor *) color
{
	//set Background Color
	[backgroundColor release];
	backgroundColor = [color retain];
	[self setNeedsDisplay: YES];
}

- (NSColor *) backgroundColor
{
	//return color
	return backgroundColor;
}

- (BOOL) isOpaque
{
	//all subviews should know, that this box has opaque color!
	//if not implemented -> problems drawing the subelement - buttons, etc.
	return YES;
}

- (void) drawRect: (NSRect) rect
{
	//dont use rect, but do complete redraw!
	NSRect bgRect = rect;//[self bounds];
    int minX = NSMinX(bgRect);
    int midX = NSMidX(bgRect);
    int maxX = NSMaxX(bgRect);
    int minY = NSMinY(bgRect);
    int midY = NSMidY(bgRect);
    int maxY = NSMaxY(bgRect) - 15;
    float radius = 10; 
    NSBezierPath *bgPath = [NSBezierPath bezierPath];
    
    // Bottom edge and bottom-right curve
    [bgPath moveToPoint:NSMakePoint(midX, minY)];
    [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY) 
                                     toPoint:NSMakePoint(maxX, midY) 
                                      radius:radius];
    
    // Right edge and top-right curve
    [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY) 
                                     toPoint:NSMakePoint(midX, maxY) 
                                      radius:radius];
	// Pointer on the top
	[bgPath lineToPoint: NSMakePoint( maxX / 2 + 15, maxY)];
	[bgPath appendBezierPathWithArcWithCenter: NSMakePoint( maxX / 2 + 15, maxY+15) radius: 15 startAngle: 270 endAngle: 180 clockwise: YES];
	[bgPath appendBezierPathWithArcWithCenter: NSMakePoint( maxX / 2 - 15, maxY+15) radius: 15 startAngle: 360 endAngle: 270 clockwise: YES];	
	[bgPath lineToPoint: NSMakePoint( maxX / 2, maxY + 15)];
	[bgPath lineToPoint: NSMakePoint( maxX / 2 - 15, maxY)];
	
    // Top edge and top-left curve
    [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY) 
                                     toPoint:NSMakePoint(minX, midY) 
                                      radius:radius];
    

	
    // Left edge and bottom-left curve
    [bgPath appendBezierPathWithArcFromPoint:bgRect.origin 
                                     toPoint:NSMakePoint(midX, minY) 
                                      radius:radius];

    [backgroundColor set];
    [bgPath fill];
}

@end
